package com.utsirc.activities;

import java.util.List;

import android.R.color;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.utsirc.R;
import com.utsirc.UTSirC;
import com.utsirc.irc.IRCService;
import com.utsirc.types.Channel;
import com.utsirc.types.ChannelState;

public class ChannelsFragment extends Fragment implements OnItemClickListener {

	public static ChannelsFragment instance;

	public ArrayAdapter<Channel> channelAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		instance = this;
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View inflateView = inflater
				.inflate(R.layout.channels, container, false);
		
		ListView lv = (ListView) inflateView.findViewById(R.id.channel_list);

		// initialize channels list
		channelAdapter = new ChannelViewAdapter(this.getActivity(),
				R.layout.channel_item_layout, UTSirC.instance.channels);
		channelAdapter.setNotifyOnChange(true);
		lv.setAdapter(channelAdapter);
		lv.setOnItemClickListener(this);

		return inflateView;
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

		IRCService.client.listChannels();
		this.refresh();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Channel c = UTSirC.instance.channels.get(position);
		UTSirC.instance.joinChannel(c);
	}

	public void refresh() {

		TextView noChannelNotification = (TextView) this.getView()
				.findViewById(R.id.no_channel);
		ListView channelList = (ListView) this.getView().findViewById(
				R.id.channel_list);

		if (UTSirC.instance.channels.size() == 0) {
			noChannelNotification.setVisibility(View.VISIBLE);
			channelList.setVisibility(View.GONE);
		} else {
			noChannelNotification.setVisibility(View.GONE);
			channelList.setVisibility(View.VISIBLE);
		}

		this.channelAdapter.notifyDataSetChanged();
	}

	class ChannelViewAdapter extends ArrayAdapter<Channel> {

		Activity context;
		int resource;
		List<Channel> channels;

		public ChannelViewAdapter(Context context, int resource,
				List<Channel> channels) {

			super(context, resource, channels);

			this.context = (Activity) context;
			this.resource = resource;
			this.channels = channels;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View v = convertView;

			if (convertView == null) {

				LayoutInflater layoutInflater = context.getLayoutInflater();
				v = layoutInflater.inflate(this.resource, parent, false);
			}

			TextView channelName = (TextView) v.findViewById(R.id.channel_name);
			ProgressBar connecting = (ProgressBar) v
					.findViewById(R.id.connecting);
			ImageView status = (ImageView) v.findViewById(R.id.status_icon);

			Channel c = channels.get(position);

			channelName.setText(c.channelName);
			if (c.connected == ChannelState.AVAILABLE) {
				status.setVisibility(View.INVISIBLE);
				connecting.setVisibility(View.INVISIBLE);
				
			} else if (c.connected == ChannelState.CONNECTING) {
				status.setVisibility(View.INVISIBLE);
				connecting.setVisibility(View.VISIBLE);

			} else if (c.connected == ChannelState.CONNECTED) {
				connecting.setVisibility(View.INVISIBLE);
				status.setVisibility(View.VISIBLE);
			
			}
			return v;
		}
	}

}
